<template>
  <el-card>
    <el-table border :data="skuArr" style="margin: 10px 0">
      <el-table-column label="序号" type="index" align="center" width="80px" />
      <el-table-column
        label="名称"
        show-overflow-tooltip
        width="150px"
        align="center"
        prop="skuName"
      ></el-table-column>
      <el-table-column
        label="描述"
        show-overflow-tooltip
        width="300px"
        prop="skuDesc"
      ></el-table-column>
      <el-table-column label="图片" width="150px" align="center">
        <template #default="{ row }">
          <img
            :src="row.skuDefaultImg"
            alt=""
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="重量"
        width="150px"
        align="center"
        prop="weight"
      ></el-table-column>
      <el-table-column
        label="价格"
        width="150px"
        align="center"
        prop="price"
      ></el-table-column>
      <el-table-column label="操作" width="250px" fixed="right" align="center">
        <template #default="{ row }">
          <el-button
            type="info"
            :icon="row.isSale === 1 ? 'Bottom' : 'Top'"
            @click="updateSale(row)"
            circle
          ></el-button>
          <el-button
            type="primary"
            icon="Edit"
            @click="updateSku"
            circle
          ></el-button>
          <el-button
            type="info"
            icon="InfoFilled"
            @click="findSku(row)"
            circle
          ></el-button>
          <el-button
            type="danger"
            icon="Delete"
            @click="removeSku(row.id)"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      :background="true"
      layout="prev, pager, next, jumper, ->, sizes, total"
      :total="total"
      @size-change="handler"
      @current-change="getHasSku"
    />
    <!-- 抽屉组件：展示商品详细 -->
    <el-drawer v-model="drawer">
      <!-- 标题部分 -->
      <template #header>
        <h4>查看商品的详细</h4>
      </template>
      <template #default>
        <el-row style="margin-top: 30px 0">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row style="margin: 30px 0">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row style="margin: 30px 0">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row
          v-if="skuInfo.skuAttrValueList && skuInfo.skuAttrValueList.length > 0"
          style="margin: 30px 0"
        >
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag
              style="margin: 5px"
              v-for="item in skuInfo.skuAttrValueList"
              :key="item.id"
            >
              {{ item.valueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row
          v-if="
            skuInfo.skuSaleAttrValueList &&
            skuInfo.skuSaleAttrValueList.length > 0
          "
          style="margin: 30px 0"
        >
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag
              style="margin: 5px"
              type="info"
              v-for="item in skuInfo.skuSaleAttrValueList"
              :key="item.id"
            >
              {{ item.saleAttrValueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row v-if="skuInfo.skuImageList" style="margin: 30px 0">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item.id"
              >
                <img
                  :src="item.imgUrl"
                  alt=""
                  style="width: 100%; height: 100%"
                />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
      <template #footer></template>
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import {
  reqSkuList,
  reqSaleSku,
  reqCancelSale,
  reqSkuInfo,
  reqRemoveSku,
} from '@/api/product/sku'
import type {
  SkuResponseData,
  SkuData,
  SkuInfoData,
} from '@/api/product/sku/type'

// 分页器当前页码
const pageNo = ref(1)
// 每一页展示的数据
const pageSize = ref(10)
// 存储已有SkU总个数
const total = ref(0)
// 存储全部SKU的数据
const skuArr = ref<SkuData[]>([])
// 控制抽屉的显示与隐藏
const drawer = ref(false)
// 存储SKU数据
const skuInfo = ref<any>({})

// 封装页面渲染函数
const getHasSku = async (pager = 1) => {
  // 当前分页器的页面
  pageNo.value = pager
  const res: SkuResponseData = await reqSkuList(pageNo.value, pageSize.value)
  if (res.code === 200) {
    total.value = res.data.total
    skuArr.value = res.data.records
  }
}
getHasSku()

const handler = () => {
  getHasSku()
}

// 商品的上架与下架操作
const updateSale = async (row: SkuData) => {
  // 如果当前商品的isSale === 1， 说明当前商品在上架状态 -> 更新为下架
  // 否则else -> 更新为上架
  if (row.isSale === 1) {
    // 下架操作
    await reqCancelSale(row.id as number)
    // 提示信息
    ElMessage.success('下架成功')
    // 发请求获取当前更新完毕的全部已有的SKU数据
    getHasSku(pageNo.value)
  } else {
    // 上架操作
    await reqSaleSku(row.id as number)
    // 提示信息
    ElMessage.success('上架成功')
    // 发请求获取当前更新完毕的全部已有的SKU数据
    getHasSku(pageNo.value)
  }
}

// 更新已有的sku数据的回调
const updateSku = () => {
  ElMessage.info('该功能尚未完成')
}

// 查看商品详细按钮的回调
const findSku = async (row: SkuData) => {
  // 展示抽屉
  drawer.value = true
  // 获取已有商品的详细数据
  const res: SkuInfoData = await reqSkuInfo(row.id as number)
  // 存储已有的sku
  skuInfo.value = res.data
  console.log(skuInfo.value)
}

// 删除某一个已有商品的回调
const removeSku = async (id: number) => {
  // 删除某一个已有的商品
  const res: any = await reqRemoveSku(id)
  if (res.code === 200) {
    ElMessage.success('删除成功')
    // 获取已有的全部商品
    getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage.error('系统数据不能删除')
  }
}
</script>

<style lang="scss" scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
